package com.app.service;

import java.util.List;

import javax.xml.bind.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.app.dao.UserGuaranteeDao;
import com.app.entity.ResidentialGuarantee;
import com.app.responseVO.PageVO;
import com.app.responseVO.ReturnResultResponseVO;
import com.app.responseVO.UserResponseVO;
@Service
public class UserGuaranteeService {

	@Autowired
	private UserGuaranteeDao userGuaranteeDao;
	
	public UserResponseVO insertUserGuarantee(ResidentialGuarantee residentialGuarantee){
		UserResponseVO responseVO = new UserResponseVO();
		responseVO.setMsg("申报成功!");
		responseVO.setStatue(true);
		try {
			int nResult = userGuaranteeDao.insertUserGuarantee(residentialGuarantee);
			if(nResult == 0){
				responseVO.setMsg("申报失败!");
				responseVO.setStatue(false);
			}
		} catch (Exception e) {
			responseVO.setMsg("申报失败!"+e.getMessage());
			responseVO.setStatue(false);
		}
		return responseVO;
	}
	
	public UserResponseVO updateUserGuarantee(ResidentialGuarantee residentialGuarantee){
		UserResponseVO responseVO = new UserResponseVO();
		responseVO.setMsg("操作成功!");
		responseVO.setStatue(true);
		try {
			int nResult = userGuaranteeDao.updateUserGuarantee(residentialGuarantee);
			if(nResult == 0){
				responseVO.setMsg("操作失败!");
				responseVO.setStatue(false);
			}
		} catch (Exception e) {
			responseVO.setMsg("操作失败!");
			responseVO.setStatue(false);
		}
		return responseVO;
	}
	public UserResponseVO deleteUserGuarantee(ResidentialGuarantee residentialGuarantee){
		UserResponseVO responseVO = new UserResponseVO();
		responseVO.setMsg("操作成功!");
		responseVO.setStatue(true);
		try {
			int nResult = userGuaranteeDao.deleteUserGuarantee(residentialGuarantee);
			if(nResult == 0){
				responseVO.setMsg("操作失败!");
				responseVO.setStatue(false);
			}
		} catch (Exception e) {
			responseVO.setMsg("操作失败!"+e.getMessage());
			responseVO.setStatue(false);
		}
		return responseVO;
	}
	public ReturnResultResponseVO<ResidentialGuarantee>  selectUserGuaranteeList(ResidentialGuarantee residentialGuarantee,int currentPage, int pageSize) throws ValidationException{
		
		ReturnResultResponseVO<ResidentialGuarantee> responseData = new ReturnResultResponseVO<ResidentialGuarantee>();
        int countPage = 0;
        int count = userGuaranteeDao.selectUserGuaranteeListNum(residentialGuarantee);
        try {
            if (count % pageSize == 0) {
                countPage = count / pageSize;
            } else {
                countPage = count / pageSize + 1;
            }
            if (currentPage < 1) {
                currentPage = 1;
            }
            if (currentPage > countPage) {
                currentPage = countPage;
            }
            PageVO pageVO = new PageVO();
            pageVO.setCountPage(countPage);
            pageVO.setCurrentPage(currentPage);
            pageVO.setPagesize(count);
            responseData.setPage(pageVO);
            List<ResidentialGuarantee> oResidentialGuarantee = userGuaranteeDao.selectUserGuaranteeList(residentialGuarantee, (currentPage - 1) * pageSize, pageSize);
            responseData.setData(oResidentialGuarantee);
        } catch (Exception e) {
        	System.out.println("hill"+e.getMessage());
            throw new ValidationException("获取列表异常");
        }
        return responseData;
	}
	public ReturnResultResponseVO<ResidentialGuarantee>  selectUserGuaranteePageList(ResidentialGuarantee residentialGuarantee,int currentPage, int pageSize) throws ValidationException{
		
		ReturnResultResponseVO<ResidentialGuarantee> responseData = new ReturnResultResponseVO<ResidentialGuarantee>();
		int countPage = 0;
		int count = userGuaranteeDao.selectUserGuaranteeListNum(residentialGuarantee);
		try {
			if (count % pageSize == 0) {
				countPage = count / pageSize;
			} else {
				countPage = count / pageSize + 1;
			}
			if (currentPage < 1) {
				currentPage = 1;
			}
			if (currentPage > countPage) {
				currentPage = countPage;
			}
			PageVO pageVO = new PageVO();
			pageVO.setCountPage(countPage);
			pageVO.setCurrentPage(currentPage);
			pageVO.setPagesize(count);
			responseData.setPage(pageVO);
			List<ResidentialGuarantee> oResidentialGuarantee = userGuaranteeDao.selectUserGuaranteeList(residentialGuarantee, (currentPage - 1) * pageSize, pageSize);
			responseData.setData(oResidentialGuarantee);
		} catch (Exception e) {
			System.out.println("hill"+e.getMessage());
			throw new ValidationException("获取列表异常");
		}
		return responseData;
	}
}
